package com.conference.controller;

import com.conference.entity.ConferenceSchedule;
import com.conference.service.ConferenceScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/conferences/{conferenceId}/schedules")
public class ConferenceScheduleController {

    @Autowired
    private ConferenceScheduleService scheduleService;

    @PostMapping
    public ResponseEntity<ConferenceSchedule> createSchedule(
        @PathVariable Long conferenceId,
        @RequestBody ConferenceSchedule schedule
    ) {
        schedule.setConferenceId(conferenceId);
        return ResponseEntity.ok(scheduleService.createSchedule(schedule));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ConferenceSchedule> updateSchedule(
        @PathVariable Long conferenceId,
        @PathVariable Long id,
        @RequestBody ConferenceSchedule schedule
    ) {
        schedule.setId(id);
        schedule.setConferenceId(conferenceId);
        return ResponseEntity.ok(scheduleService.updateSchedule(schedule));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSchedule(@PathVariable Long id) {
        scheduleService.deleteSchedule(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<ConferenceSchedule> getSchedule(@PathVariable Long id) {
        return ResponseEntity.ok(scheduleService.getScheduleById(id));
    }

    @GetMapping
    public ResponseEntity<List<ConferenceSchedule>> getSchedules(@PathVariable Long conferenceId) {
        return ResponseEntity.ok(scheduleService.getSchedulesByConferenceId(conferenceId));
    }

    @GetMapping("/venue/{venueId}")
    public ResponseEntity<List<ConferenceSchedule>> getSchedulesByVenue(
        @PathVariable Long conferenceId,
        @PathVariable Long venueId
    ) {
        return ResponseEntity.ok(scheduleService.getSchedulesByVenueId(venueId));
    }

    @GetMapping("/search")
    public ResponseEntity<Page<ConferenceSchedule>> searchSchedules(
        @PathVariable Long conferenceId,
        @RequestParam(required = false) Long venueId,
        @RequestParam(required = false) String title,
        @RequestParam(required = false) String speaker,
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime,
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "10") int size
    ) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.ASC, "startTime"));
        return ResponseEntity.ok(scheduleService.searchSchedules(
            conferenceId, venueId, title, speaker, startTime, endTime, pageable
        ));
    }
} 